package com.JavaNet.chapter8_client_socket;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import javax.net.ssl.SSLSocketFactory;

public class SocketForHttp {

    private int port;
    private String host;
    private Socket socket;
    private BufferedReader bufferedReader;
    private BufferedWriter bufferedWriter;


    public SocketForHttp(String host, int port, String protocol) throws Exception {

        this.host = host;
        this.port = port;

        if ("http".equalsIgnoreCase(protocol)) {

            this.socket = new Socket(this.host, this.port);
        } else if ("https".equalsIgnoreCase(protocol)) {

            this.socket = SSLSocketFactory.getDefault().createSocket(this.host, this.port);
        }else {
            System.out.println("未指定协议, 使用http.......");
            this.socket = new Socket(this.host, this.port);
        }
        this.socket.setSoTimeout(20 * 1000);   // mark   设置超时........

    }


    public SocketForHttp(String host, int port) throws Exception {

        new SocketForHttp(host, port, "http");

    }

    public void sendGet(String requestUrlPath) throws IOException {
        //String requestUrlPath = "/z69183787/article/details/17580325";

        if (requestUrlPath == null) {

            requestUrlPath = "/xy/expose/merDetail";
        }

        OutputStreamWriter streamWriter = new OutputStreamWriter(socket.getOutputStream());
        bufferedWriter = new BufferedWriter(streamWriter);
        bufferedWriter.write("GET " + requestUrlPath + " HTTP/1.1\r\n");
        bufferedWriter.write("Host: " + this.host + "\r\n");
        bufferedWriter.write("\r\n");
        bufferedWriter.flush();

        BufferedInputStream streamReader = new BufferedInputStream(socket.getInputStream());
        bufferedReader = new BufferedReader(new InputStreamReader(streamReader, StandardCharsets.UTF_8));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        bufferedReader.close();
        bufferedWriter.close();
        socket.close();

    }


    public void sendPost(String path) throws IOException {

        if (path == null) {
            path = "/";
        }

        String data = URLEncoder.encode("sp.lk.merchant_name", "utf-8") + "=" + URLEncoder.encode("广西", "utf-8") + "&" +
                URLEncoder.encode("sp.lk.expander", "utf-8") + "=" + URLEncoder.encode("劳维", "utf-8");
        System.out.println(">>>>>>>>>>>>>>>>>>>>>" + data);
        int length = data.getBytes().length;
        System.out.println("数据长度==> " + length    );
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8);
        bufferedWriter = new BufferedWriter(outputStreamWriter);
        bufferedWriter.write("POST " + path + " HTTP/1.1\r\n");
        bufferedWriter.write("Host: " + this.host + "\r\n");
        bufferedWriter.write("Keep-Alive: timeout=10, max=99\r\n");
        bufferedWriter.write("Accept: " +  "image/avif,image/webp"  + "\r\n");
        bufferedWriter.write("Content-Length: " +  100+ "\r\n");
        bufferedWriter.write("Content-Type: application/x-www-form-urlencoded\r\n");
        bufferedWriter.write("\r\n");
        bufferedWriter.write(data);    //如果要上传json ,  只需要把这里改成 json 字符串 , 把 Content-Type 头改成 application/json
        bufferedWriter.write("\r\n");
        bufferedWriter.flush();

        bufferedReader = new BufferedReader(
                new InputStreamReader(
                        new BufferedInputStream(
                                socket.getInputStream()
                        ), StandardCharsets.UTF_8));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        bufferedReader.close();
        bufferedWriter.close();
        socket.close();
    }

    public static void main(String[] args) throws Exception {
        /**
         * http协议测试
         */
        SocketForHttp forHttpTest = new SocketForHttp("10.208.8.6" , 8080 , "http");
        /**
         * https协议测试
         */
//        SocketForHttpTest forHttpTest = new SocketForHttpTest("zz.bdstatic.com", 443);
        try {
//            forHttpTest.sendGet();
            forHttpTest.sendPost("/xy/expose/merDetail");
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

}
